package cre.sample.test.encoding;

/**
 * @Author penglai
 * @Date 2016/10/26.
 */
public class Utf16Utils {

    public static String encodeHex(int code) {
        return Integer.toHexString(Integer.parseInt(encode(code), 2));
    }

    public static String encode(int code) {
        if (code < 0x10000) {
            return Integer.toBinaryString(code);
        } else {
            code = code - 0x10000;
            String bin = Integer.toBinaryString(code);
            int s = bin.length() - 10;
            String high = bin.substring(0, s);
            String low = bin.substring(s, bin.length());
            System.out.println(bin);

            int i1 = Integer.parseInt(high, 2) + 0xD800;
            int i2 = Integer.parseInt(low, 2) + 0xDC00;
            return Integer.toBinaryString(i1) + Integer.toBinaryString(i2);
        }
    }

}
